/*
** Small-C Compiler Version 2.7	(modified by Fred A. Scacchitti 3/26/86)
**
** Copyright 1982, 1983 J. E. Hendrix
**
**
**
**
** Version 2.6 -> 2.7 Changes   		F. A. Scacchitti 3/25/86
**
**
** - New runtime library (RDRTL) for use with compiler and to support
**   redirected output to a file. ( write > and append >> modes )
**   Supports  8 - 1k file buffers.
**   Misc. bugs cleared up.
**
** - New I/O library (CLIB) improved size/performance with new
**   available features. (statics and ? : operator)
**   Added getkbd(), putcrt() to get around redirection if desired.
**   Misc. bugs cleared up inc. JEH's #60
**
**   added nested include files, up to 6 deep
**	- CC1.C  int input2[6], inclevel
**	- CC11.C main()
**	- CC12.C doinclude()
**	- CC2.C  extern int input2[6], inclevel
**	- CC21.C inline()
**
**   fixed minor bug in doasm() undid JEH's 49
**	- CC13.C doasm()
**
**
** Version 2.5 -> 2.6 Changes   		F. A. Scacchitti 3/21/86
**
**
** - added global multidimensional arrays  eg. ara[5] [5], c[4] [3] [5] 
** 	- CC.DEF Multidimmensional Array Macros
**               added AINDEX to symbol table macros
** 	- CC1.C added *sarrptr, *arrptr, *arrcount, to track and
**              control array usage
** 	- CC11.C main()
** 	- CC12.C declglb(), newfunc()
** 	- CC3.C ext int *sarrptr, *arrptr, arrcount
** 	- CC32.C hier14()
**	- CC41.C swap2() eliminate bad optimization for MDA's
** 	- CC42.C getmd()  the code generator
** 
** - upgraded main() syntax to optimize compiler operation
** 	- CC1.C changed argvs[MAXARGS] to argvs
** 	- CC11.C main() installed standard syntax
**
**
** Version 2.4 -> 2.5 Changes		F. A. Scacchitti 3/6/86
**
**
** - added arrays of pointers eg. *argv[], ***ptr[]
** 	- CC12.C declglb(), decloc(), doargs(), newfunc()
** 	- CC12.C removed paerror()  (undid JEH's #53)
** 
** - added octal and hex constants eg 0377, 0777, 0xff, 0X12ab
** 	- CC33.C number()
**
**
** Version 2.3 -> 2.4 Changes		F. A. Scacchitti 3/4/86
**
**
** - added type FILE
** 	- stdio.h
** - restore original type at end of pointer queue for type char
** 	- CC1.C char explevel
** 	- CC12.C doargs()
** 	- CC3.C ext char explevel()
** 	- CC31.C expression(), hier12()
** 	- CC4.C ext char explevel()
** 	- CC41.C indirect(), putstk()
**
**
** Version 2.2 -> 2.3 Changes		F. A. Scacchitti 2/24/86
**
**
** - added J. E. Hendrix's fixes 
** 	- #61 Increase number and size of allowed macros
** 
** - added mutiple levels of indirection eg. **argv, ***ptr
** 	- CC.DEF modified symbol table format (added LEVEL, ITYPE)
** 	- CC1.C char level, itype
** 	- CC12.C declglb(), decloc(), doargs(), newfunc()
** 	- CC13.C addlabel()
** 	- CC21.C addsym()
** 	- CC3.C ext char level, itype
** 	- CC32.C primary()
** 	-	NOT 100% for lowest level for type char
**
**
** Version 2.1 -> 2.2 Changes		F. A. Scacchitti 2/17/86
**
**
** - changed the following to better conform to CP/M environment
**   to conform to my runtime environment
** 	- newline character back to CR (undid 45)
** 	- new ask()
** 	- new openin() & xgets() replaces openfile() & mustopen()
** 	- new doinclude()
** 	- allow listfp to display listings (undid 48)
**
** - added -i option to initialize to zero only when directed
** 	- CC1.C char iflag, CC11.C ask(), dumpzero()
** 	- CC4.C ext char iflag
** 
** - added -n option to allow return to CCP if desired
** 	- CC1.C char nbflg, CC11.C ask()
** 	- CC4.C ext char nbflg, CC41.C trailer()
** 
** - added TYPE statics to allow nonglobal static variables
** 	- CC1.C char m80flg, CC11.C ask(), parse()
** 	- CC4.C ext char m80flg, CC41.C entry()
** 
** - added the conditional operator {exp1 ? exp2 : exp3}
** 	- CC31.C hier1(), ducon()
**
** - added J. E. Hendrix's newsletter fixes 
** 	- #59 CC32.C fix optimize error
** 	- #64 CC31.C fix assignment error
**
**
** Version 2.0 -> 2.1 Change Record
**     (primed numbers keyed to text)
** 01' fix bogus label generated by "continue" within "switch"
**     (A. Macpherson)
** 02' fix problem of "peephole" missing end of staging buffer
**     (E. Payne & A. Macpherson)
** 03' permit (*func)() syntax for functions as arguments
**     (E. Payne)
** 04  change spelling of "heir" to "hier"
** 05  change spelling of "plunge" to "plnge"
** 06  always compile function "upper"
** 07' allow smaller NAMEMAX/NAMESIZE w/o truncating keywords
**     (E. Payne)
** 08' disallow local declarations inside "switch" statements
** 09' make "outdec" handle the constant 32768 properly
** 10  change CCALLOC() to calloc()
** 11  change CCAVAIL() to avail()
** 12  change CCPOLL() to poll()
** 13' install (*func)() syntax
** 14' correct extraneous operand fetch in expressions
**     like (i+5)();
** 15' make expressions like (&ia[...] - &ia[...]) scale
**     properly to give the number of objects lying between
** 16' eliminate "DW 0" generated by "int (*func)();"
** 17' "fclose" should return NULL or ERR like UNIX
** 18' "fflush" should return NULL or EOF like UNIX
** 19' "fgets" should return the newline like UNIX
** 20' remove redundant loop from "inline"  (E. Payne)
** 21  rename functions (e.g., or(), and(),
**     ret(), call(), etc.) to avoid M80 reserved words
** 22  shorten CCDDPDPI and CCDDPDPC to 6 characters
**     to satisfy L80 and LIB
** 23' use NEWLINE symbol for newline value
** 24' fix pstr() end-of-line problem
**     (A. Macpherson & M. Grundy)
** 25' allocate space for local pointer declared as ptr[]
**     (A. Macpherson)
** 26' make primary() recognize expression strings
**     (A. Macpherson)
** 27  alter code generation for M80, L80
** 28' use double colon to declare entry points
** 29  employ standard functions isalpha(), isdigit(),
**     and toupper()
** 30' drop bad optimizing case from peephole(),
**     per Paul West (DDJ #81)
** 31' supply argument for avail() to abort on stack
**     overflow
** 32' prevent preprocess() from taking newline as
**     white space
** 33' always declare "_link" external in LINK mode
**     to force loading of required library functions
** 34' restrict doubling of constants operating on
**     integer addresses to add and subtract operators
** 35' use XRA A to pass an argument count of zero
**     per Paul West (DDJ #81)
** 36' improve indirect function calls per Paul West (DDJ #81)
** 37' automatically declare undeclared functions to be
**     external
** 38  drop support of sequential macro and global table
**     searching
** 39' provide a default extension of .C to input file
**     names, and assume an output .MAC file if stdout
**     has not been redirected to a disk file
** 40  drop support of parameter prompting and drop
**     CMD_LINE
** 41  drop external function declarations from
**     cc1.c, cc2.c, cc3.c, and cc4.c
** 42' begin execution at main() rather than first function
** 43  drop tabs from the output
** 44  always compile calls to poll() in the compiler
** 47' accept #include "...." or <....> constructs
** 48' show even #asm input in the listing and prevent an
**     extra ";" at the end of the output from commenting
**     out the first EXT
** 49' correct double spacing in the output during
**     #asm ... #endasm intervals
** 50' correct lingering problem with correctly performing
**     operand fetches for indirect function calls
** 51' correct bad syntax in trailer() if LINK not defined
** 52' init locptr before first function so something like
**     "int ia[x];" where x is not #defined won't hang
** 53' generate error on attempt to declare pointer arrays
** 54' generate "EXT _link" only if "main()" is defined
** 55' eliminate redundant code generated at the end of a
**     compound statement
** 56  change system global names from leading _ to U to
**     accommodate users with pre 3.44 MACRO-80 packages
**
*/

/*
** compile options
*/
#define NOCCARGC /* no argument counts */
#define SEPARATE /* compile separately */
#define OPTIMIZE /* compile output optimizer */
#define DYNAMIC  /* allocate memory dynamically */
#define COL      /* terminate labels with a colon */
/* #define UPPER    /* force symbols to upper case */
#define LINK     /* will use with linking loader */

/*
** machine dependent parameters
*/
#define BPW     2   /* bytes per word */
#define LBPW    1   /* log2(BPW) */
#define SBPC    1   /* stack bytes per character */
#define ERRCODE 7   /* op sys return code */
 
/*
** symbol table format
*/
#define IDENT    0
#define TYPE     1
#define CLASS    2
#define LEVEL    3					/* fas 2.3 */
#define ITYPE    4					/* fas 2.3 */
#define AINDEX   5					/* fas 2.6 */
#define OFFSET   6					/* fas 2.6 */
#define NAME     8					/* fas 2.6 */
#define OFFSIZE (NAME-OFFSET)				
#define SYMAVG  15					/* fas 2.6 */
#define SYMMAX  17					/* fas 2.6 */

/*
** symbol table parameters
*/
#define NUMLOCS   25
#define STARTLOC  symtab
#define ENDLOC   (symtab+(NUMLOCS*SYMAVG))
#define NUMGLBS   200
#define STARTGLB  ENDLOC
#define ENDGLB   (ENDLOC+((NUMGLBS-1)*SYMMAX))
#define SYMTBSZ   3775  /* NUMLOCS*SYMAVG + NUMGLBS*SYMMAX   fas 2.4 */ 
 
/*
** multidimensional array  parameters			fas 2.6
*/
#define MAXELMNTS   8
#define MAXARRAYS   10 + 1		 /* 1 dummy */
#define ARRINDEX    MAXELMNTS
#define MDASIZE     MAXELMNTS * MAXARRAYS 

/*
** System wide name size (for symbols)
*/
#define NAMESIZE 9
#define NAMEMAX  8
 
/*
** possible entries for "IDENT"
*/
#define LABEL    0
#define VARIABLE 1
#define ARRAY    2
#define POINTER  3
#define FUNCTION 4
 
/*
** possible entries for "TYPE" or "ITYPE"
**    low order 2 bits make type unique within length
**    high order bits give length of object
*/
/*      LABEL   0 */
#define CCHAR   (1<<2)
#define CINT    (BPW<<2)
 
/*
** possible entries for "CLASS"
*/
/*      LABEL     0 */
#define STATIC    1
#define AUTOMATIC 2
#define EXTERNAL  3
#define AUTOEXT   4         /*37*/

/*
** "switch" table
*/

#define SWSIZ   (2*BPW)
#define SWTABSZ (60*SWSIZ)
 
/*
** "while" statement queue
*/
#define WQTABSZ  30
#define WQSIZ     3
#define WQMAX   (wq+WQTABSZ-WQSIZ)
 
/*
** entry offsets in while queue
*/
#define WQSP    0
#define WQLOOP  1
#define WQEXIT  2
 
/*
** literal pool
*/
#define LITABSZ 800
#define LITMAX  (LITABSZ-1)
 
/*
** input line
*/
#define LINEMAX  127
#define LINESIZE 128

/*
** output staging buffer size
*/
#define STAGESIZE   800
#define STAGELIMIT  (STAGESIZE-1)
 
/*
** macro (define) pool
*/
#define MACNBR   130
#define MACNSIZE (MACNBR*(NAMESIZE+2))
#define MACNEND  (macn+MACNSIZE)
#define MACQSIZE (MACNBR*7)
#define MACMAX  (MACQSIZE-1)
 
/*
** statement types
*/
#define STIF      1
#define STWHILE   2
#define STRETURN  3
#define STBREAK   4
#define STCONT    5
#define STASM     6
#define STEXPR    7
#define STDO      8 /* compile "do" logic */
#define STFOR     9 /* compile "for" logic */
#define STSWITCH 10 /* compile "switch/case/default" logic */
#define STCASE   11
#define STDEF    12
#define STGOTO   13 /* compile "goto" logic */
#define STLABEL  14 /*55*/

/*
** command line
*/

#define MAXARGS 24

